﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts.Report;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Kingdee.BOS.Core.Enums.BOSEnums;

namespace RK.ReportPlugin.ZhongRun
{
    [Description("ZhongYun：内部利润报表"), HotUpdate]
    public class NeiBuLiYunBiao : SysReportBaseService
    {
        public override void Initialize()
        {
            base.Initialize();
            this.ReportProperty.ReportType = ReportType.REPORTTYPE_NORMAL;
            this.ReportProperty.ReportName = new LocaleValue("内部利润报表", base.Context.UserLocale.LCID);
            this.IsCreateTempTableByPlugin = true;
            this.ReportProperty.IsUIDesignerColumns = false;
            this.ReportProperty.IsGroupSummary = true;
            this.ReportProperty.SimpleAllCols = false;
            this.ReportProperty.PrimaryKeyFieldName = "FMATNAME";
            this.ReportProperty.IdentityFieldName = "seq";
        }
        protected override string BuilderTempTableOrderBySQL(IRptParams filter)
        {
            string sortStr = filter.FilterParameter.SortString;
            if (sortStr.Length > 0)
            {
                return sortStr;
            }
            return " FMATNAME ";
        }
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            string seqFld = string.Format(base.KSQL_SEQ, BuilderTempTableOrderBySQL(filter));
            string createSql = @"/*dialect*/ 
            CREATE TABLE #TEMP
            (FTHORG         VARCHAR(255) NULL, 
             FIDENTITYID         INT NULL,
             FRKORG VARCHAR(255) NULL,
             FDATE           VARCHAR(50) NULL, 
             FTYPE           VARCHAR(255) NULL, 
             FMATNAME    VARCHAR(255) NULL, 
             FSPECIFICATION       VARCHAR(255) NULL, 
             FUNIT          VARCHAR(50) NULL, 
             FQTY DECIMAL(38, 10) NOT NULL, 
             FCONSIGNPRICE          DECIMAL(38, 10) NOT NULL, 
             FCONSIGNAMOUNT          DECIMAL(38, 10) NOT NULL, 
             FLY        DECIMAL(38, 10) NOT NULL
            )
             ";
            string filterStr = "";
            if (filter.FilterParameter.FilterString.Length > 0)
            {
                filterStr = " where " + filter.FilterParameter.FilterString;
            }
            string dataSql = $" INSERT INTO #TEMP EXEC P_RK_NeiBuLiYunBiao ";
            string endSql = $" select * ,{seqFld} into {tableName} from #TEMP {filterStr}";
            string sql = createSql + dataSql + endSql;
            DBUtils.ExecuteDynamicObject(this.Context, sql);
        }
        public override List<SummaryField> GetSummaryColumnInfo(IRptParams filter)
        {
            var result = base.GetSummaryColumnInfo(filter);
            result.Add(new SummaryField("FQTY", Enu_SummaryType.SUM));
            result.Add(new SummaryField("FCONSIGNAMOUNT", Enu_SummaryType.SUM));
            result.Add(new SummaryField("FLY", Enu_SummaryType.SUM));
            result.Add(new SummaryField("FCONSIGNPRICE", Enu_SummaryType.SUM));
            return result;
        }
    }
}
